package com.mohe.dangjian.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.struts2.ServletActionContext;

public class ParamsUtil {
	
	public static final String IMAGE_DIR = "pics";
	public static final String IMAGE_PATH = IMAGE_DIR+"/";
	public static final String ZIP_DIR = "zips";
	public static final String ZIP_PATH = ZIP_DIR+"/";
	
	public static boolean delFile(String path){
		String filePath = ServletActionContext.getServletContext().getRealPath(path);
		File file = new File(filePath);
		if(file.exists()){
			return file.delete();
		}else{
			return true;
		}
	}
	/** 
     * 功能:压缩多个文件成一个zip文件 
     * @param srcfile：源文件列表 
     * @param zipfile：压缩后的文件 
     */  
    public static void zipFiles(List<File> srcfile,File zipfile){  
        byte[] buf=new byte[1024];  
        ZipOutputStream out=null;
        FileInputStream in=null;
        try {  
            //ZipOutputStream类：完成文件或文件夹的压缩  
            out=new ZipOutputStream(new FileOutputStream(zipfile));  
            for(int i=0;i<srcfile.size();i++){  
                in=new FileInputStream(srcfile.get(i));  
                out.putNextEntry(new ZipEntry(srcfile.get(i).getName()));  
                int len;  
                while((len=in.read(buf))>0){  
                    out.write(buf,0,len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
             
        } catch (Exception e) {  
            e.printStackTrace();  
        }finally{
        	if(out!=null){
        		 try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
        	if(in!=null){
        		try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
        	}
        }
    }  

}
